import 'package:backend.core/core/domain/errors/failures.dart';
import 'package:dartz/dartz.dart';
import 'package:backend.core/core/domain/entities/google_play/google_play_purchase_item_type.dart';

abstract class GetGooglePlayPurchaseItemType {
  Either<GooglePlayPurchaseItemTypeGettingFailure, GooglePlayPurchaseItemType> call({
    required String purchaseItemSku,
  });
}

class GetGooglePlayPurchaseItemTypeImpl implements GetGooglePlayPurchaseItemType {
  const GetGooglePlayPurchaseItemTypeImpl();

  @override
  Either<GooglePlayPurchaseItemTypeGettingFailure, GooglePlayPurchaseItemType> call({
    required String purchaseItemSku,
  }) {
    const subscriptionSkus = {
      'com.meowofficial.words.month',
      'com.meowofficial.words.quarter',
      'com.meowofficial.words.year',
      'com.meowofficial.words.year.introduction',
      'com.meowofficial.words.2.month',
      'com.meowofficial.words.3.month.trial',
      'com.meowofficial.words.3.year.trial',
    };

    const oneTimePurchaseItemSkus = {
      'com.meowofficial.words.2.lifetime',
      'com.meowofficial.words.2.lifetime.discount',
      'com.meowofficial.words.3.lifetime',
    };

    if (subscriptionSkus.contains(purchaseItemSku)) {
      return right(GooglePlayPurchaseItemType.subscription);
    }

    if (oneTimePurchaseItemSkus.contains(purchaseItemSku)) {
      return right(GooglePlayPurchaseItemType.oneTimePurchaseItem);
    }

    return left(const GooglePlayPurchaseItemTypeGettingFailure());
  }
}

class GooglePlayPurchaseItemTypeGettingFailure extends Failure {
  const GooglePlayPurchaseItemTypeGettingFailure();
}
